﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Assignment2
{
    /// <summary>
    /// Decodes an order into a String for transport via the multi-cell buffer
    /// </summary>
    class Decoder
    {
        private String orderString;

        /// <summary>
        /// Constructor to initialize the decoder.
        /// </summary>
        /// <param name="order">String encoded order.</param>
        public Decoder(string orderString)
        {
            this.orderString = orderString;
        }

        /// <summary>
        /// Decodes the encoded string order
        /// </summary>
        /// <returns>Decoded order as an Order</returns>
        public Order decode()
        {
            Order order = null;

            byte[] orderStringBytes = Convert.FromBase64String(orderString);

            MemoryStream memoryStream = new MemoryStream(orderStringBytes);
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            order = (Order)binaryFormatter.Deserialize(memoryStream);
            memoryStream.Close();

            return order;
        }
    }
}